package com.busi.controller.activityplan;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.busi.model.Competition;
import com.busi.model.Data;
import com.busi.util.DateUtil;
import com.busi.util.HttpUtils;
import com.busi.util.Iresp_common;
import com.busi.util.PropertyUtil;
import com.busi.util.StringUtil;
import com.zcurd.controller.BaseController;


/**
 * ******************  类说明  *********************
 * class       :  IndianaController
 * @version    :  1.0  
 * description :  夺宝明细
 * @see        :     
 * @date       :  2017年5月18日 上午10点23分
 * author   :   mengxiangdong
 * ***********************************************
 */
public class IndianaController extends BaseController{
	
	public void listPage(){
		render("list.html");
	}
	
	public void listData(){
		Competition c = getBean(Competition.class, "");
		String body = gson.toJson(c);
		LOG.info("#listData() 调用夺宝明细查询接口，请求报文:" + body);
		String domain = PropertyUtil.getStrValue("indiana.domain", "");
		String url = domain + "/indiana/admin/indiana/getIndiana";
		String[][] headers = {{"Content-Type", "application/json"}};
		String respjson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");
		if (StringUtil.isNotBlank(respjson)) {
			renderJson(respjson);
		}
	}
	
	
	public void updatePage() {
		Competition c = getBean(Competition.class, "");
		c.setPage(1);
		c.setRows(1);
		String body = gson.toJson(c);
		String domain = PropertyUtil.getStrValue("indiana.domain", "");
		String url = domain + "/indiana/admin/indiana/getIndiana";
		String[][] headers = {{"Content-Type", "application/json"}};
		String respjson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");
		
		Data d=gson.fromJson(respjson, Data.class);
		setAttr("c",d.getRows().get(0));
		render("updatePage.html");
	}
	
	
	/**
	 * ******************  类说明  *********************
	 * class       :  IndianaController
	 * @author     :  mengxiangdong
	 * @version    :  1.0  
	 * description :  夺宝明细修改
	 * @throws ParseException 
	 * @see        :     
	 * @date       :  2017年5月18日 19点23分
	 * ***********************************************
	 */
	public void update() throws ParseException {
		Competition c = getBean(Competition.class, "");

		String start=c.getStart().replace("/", "").replace("-", "").replace(" ", "").replace(":", "");
		String end=c.getEnd().toString().replace("/", "").replace("-", "").replace(" ", "").replace(":", "");
	
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");//小写的mm表示的是分钟  
/*		String dstr="20080424010101";  
		java.util.Date date=sdf.parse(dstr); */ 
		
		c.setStarttime(sdf.parse(start));
		c.setEndtime(sdf.parse(end));
		
	
	
		String body = gson.toJson(c);
		LOG.info("#update() 调用夺宝明细修改接口，请求报文:" + body);
		String domain = PropertyUtil.getStrValue("indiana.domain", "");
		String url = domain + "/indiana/admin/indiana/upcom";
		String[][] headers = {{"Content-Type", "application/json"}};
		String respjson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");
		
		Iresp_common io=gson.fromJson(respjson, Iresp_common.class);
//			return io.getMsg();
			renderJson(respjson);
		
	}
	
	
	/**
	 * ******************  类说明  *********************
	 * class       :  IndianaController
	 * @author     :  mengxiangdong
	 * @version    :  1.0  
	 * description :  夺宝明细导出
	 * @see        :     
	 * @date       :  2017年5月18日 19点23分
	 * ***********************************************
	 */
	public void exportCsv() throws UnsupportedEncodingException{
		Competition competition = getBean(Competition.class, "");
		//TODO 权限过滤？
//		String merid = getMerid();
//		if(StringUtil.isNotEmpty(merid)){
//			order.setMerid(merid);
//		}
	
		String body = gson.toJson(competition);
		LOG.info("#exportCsv() 调用夺宝明细导出接口，请求报文:" + body);
		String domain = PropertyUtil.getStrValue("indiana.domain", "");
		String url = domain + "/indiana/admin/indiana/getIndiana2";
		String[][] headers = {{"Content-Type", "application/json"}};
		String respjson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");
		LOG.info("#exportCsv() 调用夺宝明细导出接口，响应报文:" + respjson);
		Data d=gson.fromJson(respjson, Data.class);
	
		LOG.info("#exportCsv() 调用夺宝明细导出接口================，响应报文:" + competition.getGoodsname());
		String filename = "夺宝方案明细" + DateUtil.getNowDate() + ".xls";
		
		
		setAttr("comList", d.getRows());
		getResponse().setContentType("application/msexcel;charset=UTF-8"); 
		getResponse().setHeader("Content-disposition","attachment; " +
				"filename=" + java.net.URLEncoder.encode(filename , "UTF-8"));
		render("export.html");
	}
	
	
	
	
	/**
	 * ******************  类说明  *********************
	 * class       :  IndianaController
	 * @author     :  mengxiangdong
	 * @version    :  1.0  
	 * description :  夺宝明细审核修改
	 * @see        :     
	 * @date       :  2017年5月18日 19点23分
	 * ***********************************************
	 */
	public void confirmIndiana() {
		Competition c = getBean(Competition.class, "");
		LOG.info("#exportCsv() 调用审核查询接口================!!!!!!!!!!!!!!!!!!!，响应报文:" +c.getId());
		setAttr("c",c);
		render("shenhe.html");
	}
	
	public void confirmIndiana2() {
		Competition c = getBean(Competition.class, "");
		String body = gson.toJson(c);
        		
		Competition cc=new Competition();
		cc.setId(c.getId());
		String body2 = gson.toJson(cc);
		String domain2 = PropertyUtil.getStrValue("indiana.domain", "");
		String url2 = domain2 + "/indiana/admin/indiana/getIndiana2";
		String[][] headers2 = {{"Content-Type", "application/json"}};
		String respjson2 = HttpUtils.doPost(url2, body2, headers2, 3, 10, "utf-8");
		LOG.info("#exportCsv() 调用审核查询接口，响应报文:" + respjson2);
		Data d=gson.fromJson(respjson2, Data.class);
		
		setAttr("comList", d.getRows().get(0).getProductid());
		
				if (StringUtil.isNotBlank(d.getRows().get(0).getProductid())) {
					Competition ccc=new Competition();
					ccc.setProductid(d.getRows().get(0).getProductid());
					
					String body3 = gson.toJson(ccc);
					String domain3 = PropertyUtil.getStrValue("indiana.domain", "");
					String url3 = domain3 + "/indiana/admin/indiana/getIndiana3";
					String[][] headers3 = {{"Content-Type", "application/json"}};
					String respjson3 = HttpUtils.doPost(url3, body3, headers3, 3, 10, "utf-8");
					Data dd=gson.fromJson(respjson3, Data.class);
					
					for (int i = 0; i < dd.getRows().size(); i++) {
						Integer auto=dd.getRows().get(i).getAuditstate();
						if(auto==3){
							renderText("3");
							return;
						}
					}
		}
		
		LOG.info("#listData() 调用夺宝明细修改接口，请求报文:" + body);
		String domain = PropertyUtil.getStrValue("indiana.domain", "");
		String url = domain + "/indiana/admin/indiana/upcom";
		String[][] headers = {{"Content-Type", "application/json"}};
		String respjson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");
		renderJson(respjson);
	}
	
	
	/**
	 * ******************  类说明  *********************
	 * class       :  IndianaController
	 * @author     :  mengxiangdong
	 * @version    :  1.0  
	 * description :  夺宝明细下架操作
	 * @see        :     
	 * @date       :  2017年5月20日 15点18分
	 * ***********************************************
	 */
	public void confirmdown() {
		Competition c = getBean(Competition.class, "");
		c.setAuditstate(4);
		String body = gson.toJson(c);
		LOG.info("#listData() 调用夺宝明细下架操作接口，请求报文:" + body);
		String domain = PropertyUtil.getStrValue("indiana.domain", "");
		String url = domain + "/indiana/admin/indiana/upcom";
		String[][] headers = {{"Content-Type", "application/json"}};
		String respjson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");

		Competition co=new Competition();
		co.setId(c.getId());
		String body2 = gson.toJson(co);
		String domain3 = PropertyUtil.getStrValue("indiana.domain", "");
		String url3 = domain + "/indiana/admin/indiana/getIndiana2";
		String[][] headers3 = {{"Content-Type", "application/json"}};
		String respjson3 = HttpUtils.doPost(url3, body2, headers, 3, 10, "utf-8");
		
		Data d=gson.fromJson(respjson3, Data.class);
		
	
		c.setDrawnum(d.getRows().get(0).getDrawnum());
		String body4 = gson.toJson(c);
		String domain2 = PropertyUtil.getStrValue("indiana.domain", "");
		String url2 = domain + "/indiana/admin/indiana/uprec";
		String respjson2 = HttpUtils.doPost(url2, body4, headers, 3, 10, "utf-8");
		renderSuccess();
		
	}
	
	
	
	/**
	 * ******************  类说明  *********************
	 * class       :  getcurrentinventory
	 * @author     :  mengxiangdong
	 * @version    :  1.0  
	 * description :  夺宝明细库存不足提示
	 * @see        :     
	 * @date       :  2017年6月5日 15点55分
	 * ***********************************************
	 */
	public void getcurrentinventory() {
		Competition competition = getBean(Competition.class, "");
	
		String body = gson.toJson(competition);
		LOG.info("#getcurrentinventory() 调用夺宝明细导出接口，请求报文:" + body);
		String domain = PropertyUtil.getStrValue("indiana.domain", "");
		String url = domain + "/indiana/admin/indiana/getIndiana4";
		String[][] headers = {{"Content-Type", "application/json"}};
		String respjson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");
		LOG.info("#getcurrentinventory() 调用夺宝明细导出接口，响应报文:" + respjson);
		renderJson(respjson);
		
	}
	
	
	
	//动态加载商品名称
	public void getGoodsName() {
		Competition competition = getBean(Competition.class, "");
		String body = gson.toJson(competition);
		LOG.info("#getGoodsName()请求参数"+competition.getGoodsname());
		if(StringUtil.isNotBlank(competition.getGoodsname())){
			String domain = PropertyUtil.getStrValue("indiana.domain", "");
			String url = domain + "/indiana/admin/indiana/getGoodsName";
			String[][] headers = {{"Content-Type", "application/json"}};
			String ret = HttpUtils.doPost(url,body,headers, 3,10 , "utf-8");
			LOG.info("#getGoodsName()响应参数========================="+ret);
			if(StringUtil.isNotBlank(ret)){
				renderJson(ret);
			}
		}
	
	}
	
	

}
